Explaining the purpose of an algorithm
Objectives : Student should be able to -
Q1. Read the following flowchart algorithm carefully.
a) Complete the trace-table for the following test data.
9, 7, 3, 12, 6, 4, 15, 2, 8, 5
A | B | C | X | Output |
0 | 0 | 100 | ||
1 | 9 | 9 | ||
2 | 7 | 7 | ||
3 | 3 | 3 | ||
4 | 12 | 12 | ||
5 | 6 | |||
6 | 4 | |||
7 | 15 | 15 | ||
8 | 2 | 2 | ||
9 | 8 | |||
10 | 5 | |||
15 2 |
b) State the purpose of the above algorithms.
⇒ The algorithm selects and outputs the Largest and the Smallest numbers from a list of 10 positive numbers.
c) Rewrite the algorithm using pseudocode.
A ← 0
B ← 0
C ← 100
REPEAT
INPUT X
IF X > B
THEN
B ← X
ELSE
IF X < C
C ← X
ENDIF
ENDIF
A ← A + 1
UNTIL A = 10
OUTPUT B, C
Q2. Look at this algorithm, shown as a flowchart :
a) Identify the process in the algorithm.
⇒ Ask to input the number of guesses the user want to make.
⇒ Ask to enter the guessed word and store it in variable "G".
⇒ If guessed word "G" is equal to the word "W", then output a congrate message for user's success.
⇒ If they don't match, then substract the number of guess by 1 and ask to guess again until number of guess becomes 0.
⇒ If the number of guess becomes 0, then output a failure message.
b) Describe the purpose of the algorithm.
To find out whether the user can guess the word within the number of guess he believe he can.
Q3. Read the following pseudocode algorithm carefully.
W ← 0
X ← 0
Y ← 100
Z ← 0
REPEAT
INPUT Mark
IF Mark < > 999
THEN
REPEAT
IF Mark < 0 OR Mark > 100
THEN
INPUT Mark
ENDIF
UNTIL Mark >= 0 AND Mark <= 100
IF Mark > X
THEN
X ← Mark
ENDIF
IF Mark < Y
THEN
Y ← Mark
ENDIF
Z ← Z + Mark
W ← W + 1
ENDIF
UNTIL Mark = 999
OUTPUT X, Y, Z
a) Use the trace table and the test data 78, 34, 22, -4, 98, 16, 734, 88, 999 to record a dry run of the above algorithm.
W | X | Y | Z | Mark | Output |
0 | 0 | 100 | 0 | 78 | |
1 | 78 | 78 | 78 | ||
2 | 112 | 34 | |||
3 | 134 | 22 | |||
-4 | |||||
4 | 98 | 232 | 98 | ||
5 | 16 | 248 | 16 | ||
734 | |||||
6 | 336 | 88 | |||
999 | 98 16 336 | ||||
b) State the purpose of the algorithm.
⇒ Accept only the integer within the range between 0 and 100 (inclusive).
⇒ Find the maximum and minimum value; calculate the running total sum of values within the range of 0 and 100.
⇒ Output the maximum, minimum and running total sum of the values within the range.
c) Rewrite the algorithm as a flowchart.
Q4. This pseudocode inputs an integer.
The predefined function DIV gives the value of the division, e.g. Y ← 10 DIV 3 gives the value Y = 3.
The predefined function MOD gives the value of the remainder, e.g. Y ← 10 MOD 3 gives the value Y = 1.
INPUT X
WHILE X > 15 DO
T1 ← X DIV 16
T2 ← X MOD 16
CASE T2 OF
10 : OUTPUT A
11 : OUTPUT B
12 : OUTPUT C
13 : OUTPUT D
14 : OUTPUT E
15 : OUTPUT F
OTHERWISE OUTPUT T2
ENDCASE
X ← T1
ENDWHILE
CASE X OF
10 : OUTPUT A
11 : OUTPUT B
12 : OUTPUT C
13 : OUTPUT D
14 : OUTPUT E
15 : OUTPUT F
OTHERWISE OUTPUT X
ENDCASE
a) Complete a trace table for each of the two input values 37 ad 191.
(i) 37
X | T1 | T2 | Output |
37 | |||
2 | 5 | 5 | |
2 | 2 | ||
(ii) 191
X | T1 | T2 | Output |
191 | |||
11 | 15 | F | |
11 | B | ||
b) State the purpose of the above algorithm.
⇒ Convert denary number into hexadecimal.
⇒ Output each digit of hexadecimal number from right to left.
Q5. This flowchart inputs five numbers and performs a calculations.
The predefined function MOD finds the remainder from integer division, for example, R ← 25 MOD 11 gives the value R a value of 3.
a) Complete the trace table for this set of input data :
5, 4, 6, 2, 1, 9, 3, 2, 1, 6, 7, 6, 1, 5, 1, 0, 0, 0, 0, 0
V | W | X | Y | Z | A | B | Output |
5 | 4 | 6 | 2 | 1 | 56 | 1 | Valid |
9 | 3 | 2 | 1 | 6 | 40 | 7 | Invalid |
7 | 6 | 1 | 5 | 1 | 61 | 6 | Invalid |
0 | 0 | 0 | 0 | 0 | |||
b) Describe the purpose of this flowchart.
⇒ Calculate the check-digit for the first four input digits.
⇒ Compares the check-digit with the fifth input digit.
⇒ If check-digit is equal to the fifth input digit, then output the message "Valid", else output the message "Invalid".
* * * * * * * * *
* * * * * *
* * *
*